using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using Fieldbook.Entities;
using Fieldbook.Common;

namespace Fieldbook.DAL
{
    public static class StorageTransactionDAO
    {
        #region insert
        private static DbCommand GetInsertCommand(SqlDatabase db)
        {
            string insertSQL = "INSERT INTO dbo.StorageTranaction (TRANSACTIONTYPEID, DATE, OPERATORID, RESOURCEID, PRICEPERUNIT, UNITS, TOTALAMOUNT) VALUES (@TRANSACTIONTYPEID, @DATE, @OPERATORID, @RESOURCEID, @PRICEPERUNIT, @UNITS, @TOTALAMOUNT) SET @STORAGETRANACTIONID = SCOPE_IDENTITY()";
            DbCommand insertCommand = db.GetSqlStringCommand(insertSQL);
            db.AddOutParameter(insertCommand, "@STORAGETRANACTIONID", DbType.Int64, 8);
            db.AddInParameter(insertCommand, "@TRANSACTIONTYPEID", DbType.Byte);
            db.AddInParameter(insertCommand, "@DATE", DbType.DateTime);
            db.AddInParameter(insertCommand, "@OPERATORID", DbType.Int64);
            db.AddInParameter(insertCommand, "@RESOURCEID", DbType.Int64);
            db.AddInParameter(insertCommand, "@PRICEPERUNIT", DbType.Decimal);
            db.AddInParameter(insertCommand, "@UNITS", DbType.Decimal);
            db.AddInParameter(insertCommand, "@TOTALAMOUNT", DbType.Decimal);
            return insertCommand;
        }
        private static void SetInsertValues(SqlDatabase db, DbCommand cmd, StorageTranaction entity)
        {
            db.SetParameterValue(cmd, "@TRANSACTIONTYPEID", (byte)entity.TransactionTypeID);
            db.SetParameterValue(cmd, "@DATE", entity.Date);
            db.SetParameterValue(cmd, "@OPERATORID", entity.OperatorID);
            db.SetParameterValue(cmd, "@RESOURCEID", entity.ResourceID);
            db.SetParameterValue(cmd, "@PRICEPERUNIT", entity.PricePerUnit);
            db.SetParameterValue(cmd, "@UNITS", entity.Units);
            db.SetParameterValue(cmd, "@TOTALAMOUNT", entity.TotalAmount);
        }
        public static bool InsertStorageTranaction(StorageTranaction entity)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            if (res)
            {
                entity.StorageTranactionID = (long?)db.GetParameterValue(cmd, "@STORAGETRANACTIONID");
            }
            return res;
        }
        public static bool InsertStorageTranaction(StorageTranaction entity, DbTransaction transactionObj, SqlDatabase db)
        {
            bool res = false;
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
            if (res)
            {
                entity.StorageTranactionID = (long?)db.GetParameterValue(cmd, "@STORAGETRANACTIONID");
            }
            return res;
        }
        #endregion
    }
}

